import numpy as np
import pandas as pd

import graphscope
from graphscope.framework.loader import Loader

# properties will be firstName,lastName,gender,birthday,creationDate,locationIP,browserUsed
graph = graphscope.g()
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/person_0_0.csv", delimiter="|"),
    label="person",
    properties=None,
)

# properties will be firstName, lastName
graph = graphscope.g()
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/person_0_0.csv", delimiter="|"),
    label="person",
    properties=["firstName", "lastName"],
)

# no properties
graph = graphscope.g()
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/person_0_0.csv", delimiter="|"),
    label="person",
    properties=[],
)

graph = graphscope.g()
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/person_0_0.csv", delimiter="|"),
    label="person",
)
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/comment_0_0.csv", delimiter="|"),
    label="comment",
)

graph = graph.add_edges(
    Loader(
        "${HOME}/.graphscope/datasets/ldbc_sample/person_likes_comment_0_0.csv",
        delimiter="|",
    ),
    label="likes",
    src_label="person",
    dst_label="comment",
    src_field="Person.id",
    dst_field="Comment.id",
)


sess = graphscope.session(cluster_type="hosts", num_workers=1, mode="lazy")
graph = sess.g()
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/person_0_0.csv", delimiter="|"),
    label="person",
)
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/comment_0_0.csv", delimiter="|"),
    label="comment",
)
graph = graph.add_vertices(
    Loader("${HOME}/.graphscope/datasets/ldbc_sample/post_0_0.csv", delimiter="|"),
    label="post",
)

graph = graph.add_edges(
    Loader(
        "${HOME}/.graphscope/datasets/ldbc_sample/person_likes_comment_0_0.csv",
        delimiter="|",
    ),
    label="likes",
    src_label="person",
    dst_label="comment",
)

graph = graph.add_edges(
    Loader(
        "${HOME}/.graphscope/datasets/ldbc_sample/person_likes_post_0_0.csv",
        delimiter="|",
    ),
    label="likes",
    src_label="person",
    dst_label="post",
)
graph = sess.run(graph)
print("Load graph from csvfile")
print(graph.schema)


leader_id = np.array([0, 0, 0, 1, 1, 3, 3, 6, 6, 6, 7, 7, 8])
member_id = np.array([2, 3, 4, 5, 6, 6, 8, 0, 2, 8, 8, 9, 9])
group_size = np.array([4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2])
e_data = np.transpose(np.vstack([leader_id, member_id, group_size]))
df_group = pd.DataFrame(e_data, columns=["leader_id", "member_id", "group_size"])
student_id = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
avg_score = np.array(
    [490.33, 164.5, 190.25, 762.0, 434.2, 513.0, 569.0, 25.0, 308.0, 87.0]
)
v_data = np.transpose(np.vstack([student_id, avg_score]))
df_student = pd.DataFrame(v_data, columns=["student_id", "avg_score"]).astype(
    {"student_id": np.int64}
)
graph = sess.g().add_vertices(df_student).add_edges(df_group)
graph = sess.run(graph)
print('Load graph from pandas')
print(graph.schema)
